package com.tdam2014.grupo02;

import java.util.ArrayList;

import com.tdam2014.grupo02.R;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.TextView;

public class CommentAdapter extends BaseAdapter {
	
	private ArrayList<Comment> list;
	private LayoutInflater inflater;

	public CommentAdapter(Context context) {
		inflater = LayoutInflater.from(context);
	}

	public void setList(ArrayList<Comment> list) {
		this.list = list;
	}

	@Override
	public int getCount() {
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		Holder holder = null;

		if (convertView == null) {
			convertView = inflater.inflate(R.layout.comment_list_item, null);
			holder = new Holder();
			holder.name = (TextView) convertView.findViewById(R.id.txtName);
			holder.message = (TextView) convertView.findViewById(R.id.txtMessage);
			holder.date = (TextView) convertView.findViewById(R.id.txtDate);

			convertView.setTag(holder);
		} else {
			holder = (Holder) convertView.getTag();
		}

		TextView name = holder.name;
		TextView message = holder.message;
		TextView date = holder.date;
		
		Comment comment = list.get(position);
		
		name.setText(comment.getCommentName());
		message.setText(comment.getCommentMessage());
		date.setText(comment.getCommentDate().toString());

		LinearLayout.LayoutParams params = (LayoutParams) holder.name
				.getLayoutParams();

		holder.name.setLayoutParams(params);
		return convertView;
	}

	private static class Holder 
	{
		TextView name, message , date;
	}

}
